// pages/dynamic/Select_topic.js
let axios = require("../../utils/util.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    toplist_hot: [],
    toplist_all: [],
    Selected: [], //已经选中id
  },
  toplist_index() {
    let params = {
      keyword: ''
    };
    axios.axios({
      url: "/record/toplist",
      data: params
    }).then((res) => {
      if (res.data.status == 0) {
        this.setData({
          toplist_hot: res.data.result.hot_list,
          toplist_all: res.data.result.all_list
        })
      }
    })
  },
  tab_active(data) {
    let item = data.currentTarget.dataset.cur[0];
    let index = data.currentTarget.dataset.cur[1];
    let hot_all = data.currentTarget.dataset.cur[2]; //1hot2all

    if (hot_all == 1) {
      let toplist_hot = "toplist_hot[" + index + "].sort_order"
      this.setData({
        [toplist_hot]: this.data.toplist_hot[index].sort_order == 0 ? '1' : '0'
      })
      if (typeof (this.contains_(this.data.toplist_all, item.id)) == 'number') {
        let toplist_all = "toplist_all[" + this.contains_(this.data.toplist_all, item.id) + "].sort_order"
        this.setData({
          [toplist_all]: this.data.toplist_all[this.contains_(this.data.toplist_all, item.id)].sort_order == 0 ? '1' : '0'
        })
      }
    } else {
      let toplist_all = "toplist_all[" + index + "].sort_order"
      this.setData({
        [toplist_all]: this.data.toplist_all[index].sort_order == 0 ? '1' : '0'
      })
      if (typeof (this.contains_(this.data.toplist_hot, item.id)) == 'number') {
        let toplist_hot = "toplist_hot[" + this.contains_(this.data.toplist_hot, item.id) + "].sort_order"
        this.setData({
          [toplist_hot]: this.data.toplist_hot[this.contains_(this.data.toplist_hot, item.id)].sort_order == 0 ? '1' : '0'
        })
      }
    }
    if (this.data.Selected.length > 0 && this.contains(this.data.Selected, item.id)) {
      // console.log('存在');
      this.data.Selected.splice(this.contains_(this.data.Selected, item.id), 1)
    } else {
      // console.log('不存在');
      this.data.Selected.push(item)
    }
    // console.log(this.data.Selected);
  },
  contains(arr, id) {
    for (var i = 0; i < arr.length; i++) {
      if (arr[i].id === id) {
        return true;
      }
    }
    return false;
  },
  contains_(arr, id) {
    for (var i = 0; i < arr.length; i++) {
      if (arr[i].id === id) {
        return i;
      }
    }
  },
  Submit() {
    if (this.data.Selected.length == 0) {
      wx.navigateBack({ //返回
        delta: 1
      })
    } else if (this.data.Selected.length > 4) {
      return wx.showToast({
        title: '您最多能选择四个话题~~',
        icon: 'none',
        duration: 3000
      })
    } else {
      var nowdata = this.data.Selected
      var queryBean = JSON.stringify(nowdata)
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        queryBean: queryBean,
      })
      wx.navigateBack({
        delta: 1,
      })
      // var nowdata = this.data.Selected
      // //将对象转为string
      // var queryBean = JSON.stringify(nowdata)
      // wx.navigateTo({
      //   url: '/pages/dynamic/release?queryBean=' + queryBean,
      // })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.toplist_index()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
